import { Data, InputData, Sheet } from './types'
import XLSX from 'xlsx'

export default function ({ data, inputs, outputs, logger, onError }: RuntimeParams<Data>) {
  inputs['input'](async (val: InputData) => {
    const wb = XLSX.utils.book_new()
    for (const dataItem of val.dataSource) {
      const sheet = XLSX.utils.json_to_sheet(dataItem.data!)
      XLSX.utils.book_append_sheet(wb, sheet, dataItem.name)
    }
    XLSX.writeFile(wb, 'out.xlsb');
    outputs['exportComplete']()
  })
}
